<?php
    class NaturalExtension_Settings {
        private $sets = array(),
                $utils;

        public function __construct($fields) {
            $this->utils = new NaturalExtension_Utils();

            foreach ($fields as $field) {
                $this->sets[] = $this->addSet($field);
            }
        }

        private function initOption($option, $value) {
            $the_option = get_option($option, 'no-option');

            if ($the_option == 'no-option') {
                add_option($option, $value, '', 'yes');
            }
        }

        private function addSet($field) {
            $prefix = $this->utils->config['opt_prefix'];

            $field['prefix'] = $prefix . $field['prefix'] . '_';

            for ($i = 0, $l = sizeof($field['fields']); $i < $l; $i += 1) {
                $field['fields'][$i][0] = $field['prefix'] . $field['fields'][$i][0];

                $this->initOption($field['fields'][$i][0], $field['fields'][$i][2]['default']);
            }

            return $field;
        }

        private function translate_elem($elem, $name) {
            $the_elem = '';

            switch ($elem['type']) {
                case 'text':
                    $the_elem = $this->elem_to_text($elem, $name);
                    break;
                case 'select':
                    $the_elem = $this->elem_to_select($elem, $name);
                    break;
                default:
                    break;
            }

            return $the_elem;
        }

        private function elem_to_select($elem, $name) {
            $select_tpl    = '<select name="%s">%s</select>';
            $option_tpl    = '<option value="%s" %s>%s</value>';
            $option_list   = array();
            $the_option    = get_option($name, $elem['default']);

            foreach ($elem['value'] as $key => $val) {
                $selected = '';

                if ($the_option == $key) {
                    $selected = 'selected';
                }

                $option_list[] = sprintf($option_tpl, $key, $selected, $val);
            }

            return sprintf($select_tpl, $name, implode('', $option_list));
        }

        private function elem_to_text($elem, $name) {
            $the_elem = '<input type="%s" name="%s" value="%s">';

            return sprintf($the_elem, $elem['type'], $name, $elem['value']);
        }

        public function displayFields($fields) {
            $tpl        = '<li><label for="%s">%s</label>%s</li>';
            $field_list = array();

            foreach ($fields as $field) {
                $form_elem = $this->translate_elem($field[2], $field[0]);

                $field_list[] = sprintf($tpl, $field[0], $field[1], $form_elem);
            }

            return implode('', $field_list);
        }

        public function displaySettings() {
            foreach ($this->sets as $set) {
                echo '<div class="settings-item">';
                    echo '<h2>' . $set['name'] . '</h2>';
                    echo '<ul>' . $this->displayFields($set['fields']) . '</ul>';
                echo '</div>';
            }
        }
    }
?>